package com.crm.util;

import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.servlet.http.HttpServletResponse;

import com.crm.model.BaseModel;
import com.crm.model.BizScale;
import com.crm.model.SysUser;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.FontSelector;
import com.lowagie.text.pdf.PdfCell;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.draw.DottedLineSeparator;

public class PDFUtil {
	
	public static void main(String[] args) throws DocumentException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, MalformedURLException, IOException {
//		SysUser s = new SysUser();
//		s.setUserId("111");
//		s.setAccount("xxx");
//		SysUser s2 = new SysUser();
//		s2.setUserId("222");
//		s2.setAccount("yyy");
//		List<SysUser> list = new ArrayList<SysUser>();
//		list.add(s2);
//		list.add(s);
		
		
		BizScale s = new BizScale();
		s.setProjectType("项目一");
		s.setProject("分配一");
		BizScale s2 = new BizScale();
		s2.setProjectType("项目二");
		s2.setProject("分配二");
		List<BizScale> list = new ArrayList<BizScale>();
		list.add(s2);
		list.add(s);
		
		PDFUtil.productPublicPDF(list,null,new BizScale());
	}
	
	/**
	 * 
	 * @Description : pdf文件导出 流程为先导出到本地然后再进行文件下载后进行文件删除操作
	 * @author zealot
	 * @param @param list
	 * @param @param response
	 * @param @param model
	 * @param @return
	 * @param @throws DocumentException
	 * @param @throws NoSuchMethodException
	 * @param @throws SecurityException
	 * @param @throws IllegalAccessException
	 * @param @throws IllegalArgumentException
	 * @param @throws InvocationTargetException
	 * @param @throws MalformedURLException
	 * @param @throws IOException
	 * @return String
	 * @version 创建时间 : 2016年11月22日 下午6:00:35
	 * @thorws
	 */
	public static String productPDF(List<? extends BaseModel> list,HttpServletResponse response,Object model) throws DocumentException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, MalformedURLException, IOException{
		
		Field[] field = model.getClass().getDeclaredFields();
		String[] modelType = new String[field.length];
		String[][] str = new String[list.size()][field.length];
		for(int k=0;k<list.size();k++){
			model = list.get(k);
			for (int i = 0; i < field.length; i++) {
				String name = field[i].getName();
				String type = field[i].getGenericType().toString();
				modelType[i] = type;
				field[i].setAccessible(true);
				name = name.replaceFirst(name.substring(0, 1), name.substring(0, 1).toUpperCase());
				Method main = model.getClass().getMethod("get" + name);
				String v =  main.invoke(model)==null?"":main.invoke(model).toString();
				str[k][i] = v;
			}
		}
		
		String[] modelName = new String[field.length];
		String[] strName = new String[field.length];
		for (int i = 0; i < field.length; i++) {
			String name = field[i].getName();
			modelName[i] = name;
			strName[i] = name;
		}
		
		Rectangle rect = new Rectangle(PageSize.B5.rotate());
		rect.setBackgroundColor(Color.ORANGE);
		String path = "/Users/zhaoyulong/Documents/bak"+(UUIDUtil.getUUID()).replaceAll("-", "");
		Document document = new Document(rect);
		PdfWriter writer =  PdfWriter.getInstance(document, new FileOutputStream(path+".pdf"));
		document.open();
		document.addTitle("zhPDF");
		document.addAuthor("zealot");
		document.add(new Paragraph("zh"));
		document.setMargins(10, 20, 30, 40); 
		
		for(int i=0;i<str.length;i++){
			PdfPTable table = new PdfPTable(3);  
			for(int k=0;k<str[i].length;k++){
				PdfPCell cell;
				Image image ;
				if(k==0){
					/**
					 * 图片需要本地添加
					 */
					image = Image.getInstance("/Users/zhaoyulong/Documents/head.gif");  
					float[] widths = { 1f, 4f }; 
					cell = new PdfPCell(new Phrase("aaaa"));  
					cell.setColspan(2);  
					table.addCell(cell); 
					table.addCell(image); 
					cell = new PdfPCell(new Phrase(""));  
					cell.setRowspan(str[i].length);  
					table.addCell(cell);  
				}else{
					table.addCell(strName[k]);  
					table.addCell(str[i][k]);  
				}
			}
			document.add(table);
			Paragraph p2 = new Paragraph("LEFT");  
			p2.add(new Chunk(new DottedLineSeparator()));  
			p2.add(String.valueOf(i));  
			document.add(p2);
		}
		document.close();
		return path+".pdf";
	}
	
	
	
	/**
	 * 
	 * @Description : 通用列表PDF导出
	 * @author zealot
	 * @param @param list
	 * @param @param response
	 * @param @param model
	 * @param @return
	 * @param @throws DocumentException
	 * @param @throws NoSuchMethodException
	 * @param @throws SecurityException
	 * @param @throws IllegalAccessException
	 * @param @throws IllegalArgumentException
	 * @param @throws InvocationTargetException
	 * @param @throws MalformedURLException
	 * @param @throws IOException
	 * @return String
	 * @version 创建时间 : 2016年11月22日 下午6:10:46
	 * @thorws
	 */
	public static String productPublicPDF(List<? extends BaseModel> list,HttpServletResponse response,Object model) throws DocumentException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, MalformedURLException, IOException{
			
			Field[] field = model.getClass().getDeclaredFields();
			String[] modelType = new String[field.length];
			String[][] str = new String[list.size()][field.length];
			for(int k=0;k<list.size();k++){
				model = list.get(k);
				for (int i = 0; i < field.length; i++) {
					String name = field[i].getName();
					String type = field[i].getGenericType().toString();
					modelType[i] = type;
					field[i].setAccessible(true);
					name = name.replaceFirst(name.substring(0, 1), name.substring(0, 1).toUpperCase());
					Method main = model.getClass().getMethod("get" + name);
					String v =  main.invoke(model)==null?"":main.invoke(model).toString();
					str[k][i] = v;
				}
			}
			
			String[] modelName = new String[field.length];
			String[] strName = new String[field.length];
			for (int i = 0; i < field.length; i++) {
				String name = field[i].getName();
				modelName[i] = name;
				strName[i] = name;
//				if(field[i].getAnnotatedType()!=null){
//					if(field[i].getAnnotation(Column.class)==null){
//						strName[i] = name;
//					}else{
//						strName[i] = field[i].getAnnotation(Column.class).name();
//					}
//				}
			}
			Rectangle rect = new Rectangle(1024f,720);
			rect.setBackgroundColor(Color.ORANGE);
			String path = "/Users/zhaoyulong/Documents/bak"+(UUIDUtil.getUUID()).replaceAll("-", "");
			Document document = new Document(rect);
			PdfWriter writer =  PdfWriter.getInstance(document, new FileOutputStream(path+".pdf"));
			document.open();
			document.addTitle("ZHPDF");
			document.addAuthor("zealot");
			document.setMargins(0, 0, 20, 10);
			/**
			 * 需要根据系统设置自己的中午编码
			 */
			BaseFont bf = null;
			try{
				bf = BaseFont.createFont("/System/Library/Fonts/ヒラギノ明朝 ProN W3.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
			}catch (Exception e) {
				bf = BaseFont.createFont("C:\\Windows\\Fonts\\simhei.ttf,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
			}
			Font font = new Font(bf);
			PdfPTable table = new PdfPTable(field.length);
			table.setWidthPercentage(100);
			table.setComplete(true);
			for(int i=0;i<str.length;i++){
				if(i==0){
					for(int k=0;k<str[i].length;k++){
						table.addCell(new Paragraph(strName[k],font));  
					}
				}
				for(int k=0;k<str[i].length;k++){
//					table.addCell(new Paragraph(strName[k],font));  
					table.addCell(new Paragraph(str[i][k],font));  
				}
//				Paragraph p2 = new Paragraph("LEFT");  
//				p2.add(new Chunk(new DottedLineSeparator()));  
//				p2.add(String.valueOf(i));  
//				document.add(p2);
			}
			document.add(table);
			document.close();
			return path+".pdf";
		}
	}
